<template>
  <ElementDialog ref="dialog" embed :visible="visible" title="添加成员" v-on="$listeners">
    <FormHolder ref="form" :disabled="disabled" label-width="130px" :model="entity">
      <InputProjectMember v-model="entity.name" placeholder="请输入姓名" prop="name" label="姓名" required @select="setupForm" />
      <FormSelect v-model="entity.gender" gender prop="gender" label="性别" required dict-type="gender"/>
      <FormDatePicker v-model="entity.birthday" prop="birthday" label="出生日期" required />
      <FormInput v-model="entity.idType" prop="idType" label="证件类型" required />
      <FormInput v-model="entity.idNo" prop="idNo" label="证件号码" required />
      <FormSelect v-model="entity.technicalTitles" prop="technicalTitles" label="专业技术职称" :options="levelOptions" required />
      <FormInput v-model="entity.duties" prop="duties" label="职务" required />
      <FormInput v-model="entity.education" prop="education" label="最高学历" required />
      <FormInput v-model="entity.speciality" prop="speciality" label="专业" required />
      <FormSelect v-model="entity.workMoth" prop="workMoth" label="投入本项目的全部工作时间（人月）" :start="1" :end="99" />
      <FormInput v-model="entity.memberType" prop="memberType" label="人员分类" />
      <FormSelect v-model="entity.hasWageIncome" prop="hasWageIncome" label="是否有工资性收入" dict-type="yes_no" />
      <FormInput v-model="entity.workUnit" prop="workUnit" label="工作单位" required />
      <!--<FormInput v-model="entity.topic" prop="topic" label="所属课题" />-->
    </FormHolder>
    <template #footer>
      <ElButton v-if="!disabled" size="mini" type="primary" @click="save">确定</ElButton>
      <ElButton size="mini" @click="close">取消</ElButton>
    </template>
  </ElementDialog>
</template>

<script>
  import { save } from './api'
  import { cloneDeep } from '@web-utils/integrations/lodash'
  import InputProjectMember from '@/module/tech-project/project-manage/member/InputProjectMember.vue'
  import { copyMember } from '@/module/tech-project/project-manage'

  export default {
    name: 'MemberEditDialog',
    components: { InputProjectMember },
    emits: ['confirm'],
    props: {
      visible: Boolean,
      saveForm: Boolean,
      disabled: Boolean,
      value: Object
    },
    data() {
      return {
        entity: {
          id: null,
          unid: null,
          createTime: null,
          createBy: null,
          createId: null,
          deptId: null,
          deptFullName: null,
          updateTime: null,
          updateId: null,
          updateBy: null,
          tenantId: null,
          delFlag: null,
          name: null,
          gender: null,
          birthday: null,
          idType: null,
          idNo: null,
          technicalTitles: null,
          duties: null,
          education: null,
          speciality: null,
          workMoth: null,
          memberType: null,
          hasWageIncome: null,
          workUnit: null,
          projectId: null,
          projectName: null,
          topic: null,
          status: null
        },
        levelOptions: [
          {
            label: '正高级',
            value: '正高级'
          },
          {
            label: '副高级',
            value: '副高级'
          },
          {
            label: '中级',
            value: '中级'
          },
          {
            label: '初级',
            value: '初级'
          },
          {
            label: '其他',
            value: '其他'
          }
        ]
      }
    },
    watch: {
      value: {
        immediate: true,
        handler(newVal) {
          if (newVal) {
            this.entity = cloneDeep(newVal)
          } else {
            this.entity = this.$options.data.call(this).entity
          }
        }
      }
    },
    methods: {
      async save() {
        await this.$refs.form.validate()
        if (this.saveForm) {
          await save(this.entity)
        }
        this.$emit('confirm', this.entity)
        this.close()
      },
      setupForm(source) {
        copyMember(this.entity, source)
      },
      close() {
        this.$refs.dialog.hide()
      }
    }
  }
</script>
